#include"front/token.h"

#include<cassert>

std::string frontend::toString(frontend::TokenType type){
    switch (type) {
    case TokenType::IDENFR: return "IDENFR";
    case TokenType::INTLTR: return "INTLTR";
    case TokenType::FLOATLTR: return "FLOATLTR";
    case TokenType::CONSTTK: return "CONSTTK";
    case TokenType::VOIDTK: return "VOIDTK";
    case TokenType::INTTK: return "INTTK";
    case TokenType::FLOATTK: return "FLOATTK";
    case TokenType::IFTK: return "IFTK";
    case TokenType::ELSETK: return "ELSETK";
    case TokenType::WHILETK: return "WHILETK";
    case TokenType::CONTINUETK: return "CONTINUETK";
    case TokenType::BREAKTK: return "BREAKTK";
    case TokenType::RETURNTK: return "RETURNTK";
    case TokenType::PLUS: return "PLUS";
    case TokenType::MINU: return "MINU";
    case TokenType::MULT: return "MULT";
    case TokenType::DIV: return "DIV";
    case TokenType::MOD: return "MOD";
    case TokenType::LSS: return "LSS";
    case TokenType::GTR: return "GTR";
    case TokenType::COLON: return "COLON";
    case TokenType::ASSIGN: return "ASSIGN";
    case TokenType::SEMICN: return "SEMICN";
    case TokenType::COMMA: return "COMMA";
    case TokenType::LPARENT: return "LPARENT";
    case TokenType::RPARENT: return "RPARENT";
    case TokenType::LBRACK: return "LBRACK";
    case TokenType::RBRACK: return "RBRACK";
    case TokenType::LBRACE: return "LBRACE";
    case TokenType::RBRACE: return "RBRACE";
    case TokenType::NOT: return "NOT";
    case TokenType::LEQ: return "LEQ";
    case TokenType::GEQ: return "GEQ";
    case TokenType::EQL: return "EQL";
    case TokenType::NEQ: return "NEQ";
    case TokenType::AND: return "AND";
    case TokenType::OR: return "OR";
    default:
        assert(0 && "invalid token type");
        break;
    }
    return "";
}

frontend::Token::Token(TokenType type, std::string value): type(type), value(value) {}

frontend::TokenType frontend::typeof_keyword(std::string s) {
    if (s == "const")
        return TokenType::CONSTTK;
    if (s == "void")
        return TokenType::VOIDTK;
    if (s == "int")
        return TokenType::INTTK;
    if (s == "float")
        return TokenType::FLOATTK;
    if (s == "if")
        return TokenType::IFTK;
    if (s == "else")
        return TokenType::ELSETK;
    if (s == "while")
        return TokenType::WHILETK;
    if (s == "continue")
        return TokenType::CONTINUETK;
    if (s == "break")
        return TokenType::BREAKTK;
    if (s == "return")
        return TokenType::RETURNTK;
    return TokenType::NONE;
}

frontend::TokenType frontend::typeof_op(std::string s) {
    if (s == "+")
        return TokenType::PLUS;
    if (s == "-")
        return TokenType::MINU;
    if (s == "*")
        return TokenType::MULT;
    if (s == "/")
        return TokenType::DIV;
    if (s == "%")
        return TokenType::MOD;
    if (s == "<")
        return TokenType::LSS;
    if (s == ">")
        return TokenType::GTR;
    if (s == ":")
        return TokenType::COLON;
    if (s == "=")
        return TokenType::ASSIGN;
    if (s == ";")
        return TokenType::SEMICN;
    if (s == ",")
        return TokenType::COMMA;
    if (s == "(")
        return TokenType::LPARENT;
    if (s == ")")
        return TokenType::RPARENT;
    if (s == "[")
        return TokenType::LBRACK;
    if (s == "]")
        return TokenType::RBRACK;
    if (s == "{")
        return TokenType::LBRACE;
    if (s == "}")
        return TokenType::RBRACE;
    if (s == "!")
        return TokenType::NOT;
    if (s == "<=")
        return TokenType::LEQ;
    if (s == ">=")
        return TokenType::GEQ;
    if (s == "==")
        return TokenType::EQL;
    if (s == "!=")
        return TokenType::NEQ;
    if (s == "&&")
        return TokenType::AND;
    if (s == "||")
        return TokenType::OR;
    return TokenType::NONE;
}